package nu.aing.P20140701;

import java.util.Arrays;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class CostOfDancing {
    
    public static void main(String[] args) {
        CostOfDancing obj = new CostOfDancing();
        TestUtils.test(obj, "minimum",
                2, 
                new int[] {1, 5, 3, 4}
        );
        TestUtils.test(obj, "minimum",
                3, 
                new int[] {1, 5, 4}
        );
        TestUtils.test(obj, "minimum",
                1, 
                new int[] {2, 2, 4, 5, 3}
        );
        TestUtils.test(obj, "minimum",
                39, 
                new int[] {973, 793, 722, 573, 521, 568, 845, 674, 595, 310, 284, 794, 913, 93, 129, 758, 108, 433, 181, 163, 96, 932,
                        703, 989, 884, 420, 615, 991, 364, 657, 421, 336, 801, 142, 908, 321, 709, 752, 346, 656, 413, 629, 801}
        );
    }
    
    public int minimum(int K, int[] danceCost) {
        return min(false, 0, 0, K--, danceCost);
    }
    
    private int min(boolean sort, int total, int index, int K, int... danceCost) {
        if(!sort) {
            Arrays.sort(danceCost);
        }
        
        return (K-- > 0) ? min(true, total += danceCost[index++], index, K, danceCost): total;
    }
}